<template>
  <div style="width:90%;" id="giftActivity">
    <el-form :model="planForm" :rules="planRules" ref="planForm" label-width="150px">
      <el-button @click="goback()" size="mini" type="primary" style="margin-left:1000px">返回</el-button>
      <el-input v-model="planForm.uuid" type="hidden"></el-input>

      <el-form-item label="积分兑换活动名称：" prop="activityName">
        <el-input v-model="planForm.activityName" :disabled="disabledFlag" style="width:360px;" placeholder="积分兑换活动名称" maxlength="30" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="活动起始时间：" prop="runTimeRange">
        <el-date-picker
          v-model="planForm.runTimeRange"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
          :clearable="false"
          :disabled="disabledFlag"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="活动分享页主图:" prop="imgListone">
          <image-upload v-model="planForm.imgListone" :size="'400x400'" :limit="1" v-if="!disabledFlag"></image-upload>
          <img :src="planForm.imgListone[0]" style="width: 180px;height: 180px;" v-if="disabledFlag"></img>
      </el-form-item>
          <el-form-item label="活动受邀页主图:" prop="imgListtwo">
          <image-upload v-model="planForm.imgListtwo" :size="'400x400'" :limit="1" v-if="!disabledFlag"></image-upload>
          <img :src="planForm.imgListtwo[0]" style="width: 180px;height: 180px;" v-if="disabledFlag"></img>
      </el-form-item>-->
      <el-form-item label="兑换礼品：" prop="planCouponList">
        <div class="giftTable">
          <el-table
            ref="giftTableone"
            :data="planCouponList"
            @selection-change="planCouponChecked"
            border
            type="expand"
            style="font-size: 10px;width:90%;"
            :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          >
            <el-table-column type="expand">
              <template slot-scope="props">
                <div class="giftTable">
                  <!--赠品列表-->
                  <el-table ref="giftTable" tooltip-effect="dark" :data="props.row.giftDetailList" width="100%">
                    <el-table-column type="index" width="60" label="序号" align="center"></el-table-column>

                    <el-table-column label="开始时间" align="center" width="270">
                      <template slot-scope="scope">
                        <el-time-picker value-format="HH:mm:ss" arrow-control v-model="scope.row.buyBeginTime" :disabled="disabledFlag" placeholder="选择开始时间"></el-time-picker>
                      </template>
                    </el-table-column>

                    <el-table-column label="结束时间" align="center" width="270">
                      <template slot-scope="scope">
                        <el-time-picker
                          arrow-control
                          value-format="HH:mm:ss"
                          v-model="scope.row.buyEndTime"
                          :disabled="disabledFlag"
                          @change="selectchange(scope.row, scope.$index, props.row, props.$index)"
                          placeholder="任意时间点"
                        ></el-time-picker>
                      </template>
                    </el-table-column>

                    <el-table-column label="库存" align="center" width="150">
                      <template slot-scope="scope">
                        <el-input
                          type="number"
                          :min="1"
                          :disabled="disabledFlag"
                          oninput="value=value.replace(/[^0-9]/g,'')"
                          v-model="scope.row.giftQuantity"
                          style="width:130px; text-align-last:right;"
                          v-on:blur="giftQuantityCheckblur(scope.row, scope.$index, props.row, props.$index)"
                        >
                          <template slot="append">
                            件
                          </template>
                        </el-input>
                      </template>
                    </el-table-column>

                    <el-table-column label="个人限购" align="center" width="150">
                      <template slot-scope="scope">
                        <el-input
                          type="number"
                          :disabled="disabledFlag"
                          :min="1"
                          oninput="value=value.replace(/[^0-9]/g,'')"
                          v-model="scope.row.buyLimitQuantity"
                          style="width:130px; text-align-last:right;"
                        >
                          <template slot="append">
                            件
                          </template>
                        </el-input>
                      </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" fixed="right" v-if="!disabledFlag">
                      <template slot-scope="scope">
                        <el-button size="mini" @click="deleteGiftsItem(scope.row, scope.$index, props.row, props.$index)" type="text">删除</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </template>
            </el-table-column>
            <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
            <el-table-column prop="giftCode" label="礼品编码" align="center" width="150"></el-table-column>
            <el-table-column prop="giftName" label="礼品名称" align="center" width="150"></el-table-column>
            <el-table-column prop="giftType" label="礼品类型" align="center" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.giftType == '1'">实物礼品</span>
                <span v-if="scope.row.giftType == '2'">卡券礼品</span>
              </template>
            </el-table-column>
            <el-table-column prop="giftType" label="会员等级" align="center" width="350">
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.memberLevelIdList"
                  @change="changeLeve(scope.row, scope.$index)"
                  multiple
                  placeholder="请选择"
                  :disabled="disabledFlag"
                  style="width:330px"
                >
                  <el-option label="所有会员" value="1"></el-option>
                  <el-option v-for="item in optionsarray" :key="item.uuid" :label="item.gradelevelName" :value="item.uuid"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column prop="giftType" label="兑换积分" align="center" width="150">
              <template slot-scope="scope">
                <el-input
                  type="number"
                  :min="0"
                  :disabled="disabledFlag"
                  v-model="scope.row.consumeBonuspoint"
                  v-on:blur="giftSumQuantityCheckone(scope.row.consumeBonuspoint, scope.$index)"
                ></el-input>
              </template>
            </el-table-column>
            <!-- <el-table-column prop="deliveryType" label="配送方式" align="center" width="150">
                  <template slot-scope="scope">
                    <span type="primary" v-if="scope.row.deliveryType == '1'">五粮液总部配送</span>
                  </template>
            </el-table-column>-->
            <el-table-column label="礼品库存" align="center" width="250">
              <template slot-scope="scope">
                <!-- <el-radio-group
                    v-model="scope.row.isLimitQuantity"
                    size="small"
                    @change="changeIsLimitQuantity(scope.row,scope.$index)"
                >
                    -->
                <!-- <el-radio-button label="1" class="el-radio-button-inner-child"> -->
                <el-input
                  :disabled="disabledFlag"
                  type="number"
                  :min="1"
                  oninput="value=value.replace(/[^0-9]/g,'')"
                  v-model="scope.row.giftQuantity"
                  style="width:80px;text-align-last:right;"
                  @focus="giftSumQuantityCheck(scope.row, scope.$index)"
                  v-on:blur="giftSumQuantityCheckblur(scope.row, scope.$index)"
                ></el-input>
                <!-- </el-radio-button>
                <el-radio-button label="0" :disabled="disabledFlag">不限</el-radio-button>
            </el-radio-group> -->
              </template>
            </el-table-column>
            <!-- <el-table-column label="赠品数量" align="center" width="150" v-if="disabledFlag">
                  <template slot-scope="scope">
                    <span v-if="scope.row.isLimitQuantity == '1'">{{scope.row.giftCount}}</span>
                    <span v-if="scope.row.isLimitQuantity == '0'">不限</span>
                  </template>
            </el-table-column>-->

            <!-- <el-table-column label="赠送数量" align="center" width="150">
                  <template slot-scope="scope">
                    <el-input
                        type="number"
                        :min="1"
                        oninput="value=value.replace(/[^0-9]/g,'')"
                        v-model="scope.row.giveCount"
                        style="width:150px; text-align-last:right;"
                        v-on:blur="giveCountCheck(scope.row,scope.$index)"
                        :disabled="disabledFlag"
                    ><template slot="append">
                        <span slot="append" v-if="scope.row.giftStyle ==1">件</span>
                        <span slot="append" v-if="scope.row.giftStyle ==2">张</span>
                        <span slot="append" v-if="scope.row.giftStyle ==3">次</span>
                        <span slot="append" v-if="scope.row.giftStyle ==4">次</span>
                        <span slot="append" v-if="scope.row.giftStyle ==5">积分</span>
                       </template>
                    </el-input>
                  </template>
            </el-table-column>-->
            <!-- <el-table-column label="有效期类型" align="center" width="150">
                  <template slot-scope="scope">
                    <el-radio-group v-model="scope.row.validDateType" size="small"
                      v-if="!disabledFlag && scope.row.giftStyle != '1' && scope.row.giftStyle != '3'" >

                        <el-radio-button label="1">天数</el-radio-button>
                        <el-radio-button label="2">日期</el-radio-button>
                    </el-radio-group>
                    <span v-if="disabledFlag && scope.row.giftStyle != '1' && scope.row.giftStyle != '3' && scope.row.validDateType == '1'">天数</span>
                    <span v-if="disabledFlag && scope.row.giftStyle != '1' && scope.row.giftStyle != '3' && scope.row.validDateType == '2'">日期</span>
                    <span v-if="scope.row.giftStyle == '1' || scope.row.giftStyle == '3'">—</span>
                  </template>
                </el-table-column>
                <el-table-column label="有效期" align="center" width="270">
                  <template slot-scope="scope">
                    <el-input v-if="scope.row.giftStyle != '1' && scope.row.giftStyle != '3' && scope.row.validDateType == 1"
                        type="number"
                        :min="1"
                        oninput="value=value.replace(/[^0-9]/g,'')"
                        v-model="scope.row.dayCount"
                        style="width:150px; text-align-last:right;"

                        :disabled="disabledFlag"
                    ><template slot="append">天</template>
                    </el-input>
                    <el-date-picker v-if="scope.row.giftStyle != '1' && scope.row.giftStyle != '3' && scope.row.validDateType == 2"
                        v-model="scope.row.validDateRange"
                        type="daterange"
                        align="right"
                        style="width:250px"
                        unlink-panels
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        value-format="yyyy-MM-dd"
                        :picker-options="pickerOptions"

                        :clearable="false"
                        :disabled="disabledFlag"
                      ></el-date-picker>
                      <span v-if="scope.row.giftStyle == '1' || scope.row.giftStyle == '3'">—</span>
                  </template>
                </el-table-column>
                <el-table-column prop="status" label="状态" align="center" width="150">
                  <template slot-scope="scope">
                      <span v-if="scope.row.status == '1'">启用</span>
                      <span v-if="scope.row.status == '0'">弃用</span>
                  </template>
            </el-table-column>-->

            <el-table-column label="购酒券组合" align="center" width="400px">
              <template slot-scope="scope">
                <div v-if="Number(scope.row.giftType) === 2">
                  <el-select :disabled="disabledFlag" v-model="scope.row.activityGroupDTOS_copy" style="width:330px" multiple placeholder="请选择可组合的购酒券">
                    <el-option
                      v-for="item in gouJiu_options"
                      :disabled="item.couponId == scope.row.giftId"
                      :key="item.couponId"
                      :label="item.couponName"
                      :value="item.couponId"
                    ></el-option>
                  </el-select>
                </div>
                <div v-if="Number(scope.row.giftType) === 1">/</div>
              </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="200px" v-if="!disabledFlag">
              <template slot-scope="scope">
                <el-button size="mini" @click="delprice(scope.row, scope.$index)" type="text">删除礼品</el-button>
                <el-button size="mini" @click="addshijian(scope.row, scope.$index)" type="text">添加时间段</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div style="margin-top:10px;"><el-button type="primary" @click="selectCoupon('planForm')" v-if="!disabledFlag">添加礼品</el-button></div>
      </el-form-item>
      <el-form-item label="发布状态：" prop="status">
        <el-radio-group v-model="planForm.status" :disabled="disabledFlag">
          <el-radio-button label="0">不发布</el-radio-button>
          <el-radio-button label="1">发布</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="积分兑换活动说明:" prop="activityDesc">
        <el-input
          type="textarea"
          v-model="planForm.activityDesc"
          :disabled="disabledFlag"
          placeholder="请输入活动描述"
          maxlength="120"
          show-word-limit
          :autosize="{ minRows: 2 }"
          style="width:650px;"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('planForm')" v-if="!disabledFlag">保 存</el-button>
        <el-button @click="resetForm('planForm')">取 消</el-button>
      </el-form-item>
    </el-form>

    <el-dialog title="选择礼品" :visible.sync="dialogVisibleCoupon" width="55%">
      <el-form :inline="true" :model="formCoupon" class="search-form-inline" style="margin-top:-20px;">
        <el-form-item label="礼品编码："><el-input v-model="formCoupon.giftCode" clearable placeholder style="width:200px"></el-input></el-form-item>
        <el-form-item label="礼品名称："><el-input v-model="formCoupon.giftName" clearable placeholder style="width:200px"></el-input></el-form-item>
        <el-form-item label="礼品类型：">
          <el-select v-model="formCoupon.giftType">
            <el-option label="实物礼品" value="1"></el-option>
            <el-option label="卡券礼品" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-button type="primary" size="mini" icon="el-icon-search" @click="getCouponList()">查询</el-button>
      </el-form>
      <el-table
        ref="singleTable"
        :data="couponList"
        @selection-change="couponChecked"
        border
        style="font-size: 10px;width:100%;"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      >
        <el-table-column type="selection" width="60" align="center" fixed disabled></el-table-column>
        <!-- <el-table-column prop="giftPic" label="赠品图片" align="center" width="120">
          <template slot-scope="scope">
              <img
                :src="scope.row.giftPic"
                v-if="scope.row.giftPic !='' && scope.row.giftPic !=null"
                width="70"
                height="50"
              />
          </template>
        </el-table-column>-->
        <!-- <el-table-column prop="giftType" label="赠品类型" align="center" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.giftType == '1'">实物赠品</span>
            <span v-if="scope.row.giftType == '2'">虚拟赠品</span>
            <span v-if="scope.row.giftType == '3'">虚拟赠品</span>
          </template>
        </el-table-column>-->
        <!-- <el-table-column prop="giftProperty" label="赠品属性" align="center" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.giftProperty != '1' && scope.row.giftProperty != '2'">-</span>
            <span v-if="scope.row.giftProperty == '1'">普通赠品</span>
            <span v-if="scope.row.giftProperty == '2'">特殊赠品</span>
          </template>
        </el-table-column>-->
        <el-table-column prop="giftCode" label="礼品编码" align="center" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column prop="giftName" label="礼品名称" align="center" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column prop="giftType" label="礼品类型" align="center" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.giftType == '1'">实物礼品</span>
            <span v-if="scope.row.giftType == '2'">卡券礼品</span>
          </template>
        </el-table-column>
        <el-table-column prop="giftDesc" label="礼品详情" align="center" show-overflow-tooltip></el-table-column>
      </el-table>
      <div class="block" style="margin-top:22px;overflow:hidden">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 20, 30, 50, 100]"
          :current-page="listQuery.current"
          :page-size="listQuery.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total3"
        ></el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleCoupon = false">取 消</el-button>
        <el-button type="primary" @click="couponConfirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getTimeSlot, qryShareActiviteInfo, queryChooseGiftsList, saveGiftActivityWithGoods } from '@/api/IntegralActivity';
import imageUpload from '@/page/common/Image-upload';
import { getmemberrule } from '@/api/membergrade';

const curDateStr = function() {
  var curDate = new Date();
  var year = curDate.getFullYear();
  var month = curDate.getMonth() + 1;
  month = month > 9 ? month : '0' + month;
  var date = curDate.getDate();
  date = date > 9 ? date : '0' + date;
  return year + '-' + month + '-' + date;
};
export default {
  data() {
    return {
      gouJiu_options: [
        {
          value: 1,
          label: '20元系列券'
        },
        {
          value: 2,
          label: '100元五粮液系列券'
        },
        {
          value: 3,
          label: '200元五粮液系列券'
        }
      ],
      buyBeginTime: new Date(2016, 9, 10, 18, 40),
      buyEndTime: new Date(2016, 9, 10, 18, 40),
      keepAlive: true,
      memberLevelIdList: [],
      logininfo: {},
      orgList: [],
      //   orgSeq: "",
      loading: true,
      typeOptions: [{ value: '2', label: '虚拟礼品' }, { value: '1', label: '实物礼品' }],
      lsPlanCouponList: [],
      lsCouponList: [],
      planCouponList: [],
      couponList: [],
      planForm: {
        uuid: '',
        giftType: '2',
        giftProperty: '1',
        activityName: '',
        activityDesc: '',
        // beginTime: "",
        // endTime: "",
        status: '',
        bonuspointConvertActivityGiftS: [],
        // imgListone: [],
        //    imgListtwo: [],
        // sharePic: "",
        // invitedPic:"",
        giftId: '',
        status: '0',
        runTimeRange: [curDateStr(), curDateStr()]
      },
      giftDetailList: [],
      optionsarray: [],
      formCoupon: {
        couponName: '',
        giftCode: '',
        giftName: '',
        giftType: '1'
      },
      timeList: [],
      dialogVisibleCoupon: false,
      disabledFlag: false,
      disabledFlagone: true,
      listQuery: {
        current: 1,
        size: 10
      },
      numone: '',
      total3: 0,
      shijianflag: true,
      numtime: 0,
      total: 0,
      pickerOptions: {
        shortcuts: [
          {
            text: '未来3天',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 3);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '未来一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '未来一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }
        ],
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7; //如果当天可选，就不用减8.64e7
        }
      },
      planRules: {
        activityName: [{ required: true, message: '分享活动名称', trigger: 'blur' }],
        status: [{ required: true, message: '请选择发布状态', trigger: 'blur' }],
        giftType: [{ required: true, message: '请选择赠品类型', trigger: 'blur' }],
        giftName: [{ required: true, message: '请输入赠品名称', trigger: 'blur' }],

        // planCouponList: [{ required: true, message: "请选择赠品", trigger: "blur" }],
        activityDesc: [{ required: true, message: '请输入活动描述', trigger: 'blur' }],
        imgListone: [{ required: true, message: '', trigger: 'blur' }],
        imgListtwo: [{ required: true, message: '', trigger: 'blur' }],
        //  planCouponList: [{ required: true, message: "", trigger: "blur" }],
        runTimeRange: [{ required: true, message: '请选择活动有效期', trigger: 'blur' }]
      }
    };
  },
  components: {
    imageUpload
  },
  mounted() {
    this.logininfo = localStorage.getItem('logininfo');
    this.orgList = JSON.parse(this.logininfo).orgList;
    // this.orgSeq = this.orgList[0].orgSeq;
    // this.planForm.orgSeq = this.orgList[0].orgSeq;
    this.disabledFlag = this.$route.params.disabledFlag;
    this.disabledFlagone = this.$route.params.disabledFlagone;
    var v = this.$route.params.edit;
    console.log(v, 'iiiiiiii');
    if (v) {
      // 修改，回显值

      this.qryShareActiviteInfo(v.uuid);
    } else {
      // 新增，默认值
      this.planForm.uuid = '';
      // this.planForm.status = "1";
      this.getCouponList();
    }
    this._getmemberinfo();
  },
  methods: {
    selectchange(row, index, prow, pindex) {
      this.planCouponList[pindex].giftDetailList.forEach((item, i) => {
        this.timeList.push({
          date: i,
          startArea: this.planCouponList[pindex].giftDetailList[i].buyBeginTime,
          endArea: this.planCouponList[pindex].giftDetailList[i].buyEndTime
        });
      });
      getTimeSlot(this.timeList).then(res => {
        console.log(res.result, 'o');

        if (this.planCouponList[pindex].giftDetailList[index].buyEndTime < this.planCouponList[pindex].giftDetailList[index].buyBeginTime) {
          this.$message({
            message: '结束时间不能小于开始时间',
            type: 'warning'
          });
          this.planCouponList[pindex].giftDetailList[index].buyEndTime = '';
        }

        if (res.result.length == 0) {
          // this.planCouponList[index].giftDetailList.push({});
          this.shijianflag = true;
        } else {
          this.$message({
            message: '当前已选区间存在重复时间',
            type: 'warning'
          });

          this.shijianflag = false;
        }
      });

      // this.planCouponList[index].giftList

      this.timeList = [];

      // if(timeArr.length > 1){
      // 	for(let i = 0;i<timeArr.length;i++){
      // 		for(let j = 0;j<timeArr.length;j++){
      // 			if(i == j){
      // 				continue
      // 			}else{
      // 				if(timeArr[i][0] <= timeArr[j][1] && timeArr[i][0] >= timeArr[j][0]){
      // 				  this.$message({
      //           message: "当前已选区间存在重复时间",
      //           type: "warning"
      //         });
      // 					break
      // 				}else if(timeArr[i][1] >= timeArr[j][0] && timeArr[i][1] <= timeArr[j][1]){
      // 				  this.$message({
      //           message: "当前已选区间存在重复时间",
      //           type: "warning"
      //         });
      // 					break
      // 				}else if(timeArr[i][0] <= timeArr[j][0] && timeArr[i][1] >= timeArr[j][1]){
      // 				  this.$message({
      //           message: "当前已选区间存在重复时间",
      //           type: "warning"
      //         });
      // 					break
      // 				}else if(timeArr[i][0] >= timeArr[j][0] && timeArr[i][1] >= timeArr[j][1]){
      // 			  this.$message({
      //           message: "当前已选区间存在重复时间",
      //           type: "warning"
      //         });
      // 					break
      // 				}
      // 			}
      // 		}
      // 	}
      // }
    },
    getTimeSecond(time) {
      let arr = time.split(':');
      let a = Number(arr[0]) * 60 * 60;
      let b = Number(arr[1]) * 60;
      let c = Number(arr[2]);
      let result = a + b + c;
      return result;
    },

    deleteGiftsItem(row, index, prow, pindex) {
      this.planCouponList[pindex].giftDetailList.splice(index, 1);
      this.planCouponList[pindex].giftQuantity = 0;
      this.planCouponList[pindex].giftDetailList.forEach((item, i) => {
        //  this.numone+=parseInt(this.planCouponList[pindex].giftDetailList[index].giftQuantity  });
        console.log(item.giftQuantity, '9999');
        this.planCouponList[pindex].giftQuantity += parseInt(item.giftQuantity);
      });
      //   this.planCouponList[pindex].consumeBonuspoint= this.planCouponList[pindex].isLimitQuantity
      console.log(this.planCouponList[pindex].giftQuantity, '99');
    },

    addshijian(row, index) {
      console.log(row, index, this.planCouponList[index].giftDetailList, 'oo');

      this.planCouponList[index].giftDetailList.push({});
      let $table = this.$refs.giftTableone;
      $table.toggleRowExpansion(row, true);
    },
    _getmemberinfo() {
      let data = {
        size: this.listQuery.size,
        current: this.listQuery.current,
        condition: {},
        ascs: [],
        descs: []
      };
      getmemberrule(data).then(res => {
        this.optionsarray = res.records;
      });
    },

    // giveCountCheck(row, index) {
    //   let val = row.giveCount;
    //   if (val === null || val === undefined || val === "") {
    //     this.$message({showClose: true, message: "礼品库存填入数量不能为空",type: "warning"});
    //     val = 1;
    //   } else if (isNaN(val)){
    //     this.$message({showClose: true, message: "礼品库存填入数量格式错误",type: "warning"});
    //     val = 1;
    //   } else if (val < 1){
    //     this.$message({showClose: true, message: "礼品库存填入数量应大于0",type: "warning"});
    //     val = 1;
    //   } else if( this.planCouponList[index].isLimitQuantity == '1' && val > this.planCouponList[index].giftQuantity){
    //     this.$message({showClose: true, message: "填入数不应大于赠送数",type: "warning"});
    //     val = this.planCouponList[index].giftQuantity;
    //   }
    //    this.planCouponList[index].giveCount = parseInt(val);
    //   return true;
    // },
    //礼品总数量
    giftQuantityCheckblur(row, index, prow, pindex) {
      this.planCouponList[pindex].giftQuantity = 0;

      console.log(this.planCouponList[pindex].giftDetailList, 'oo');
      this.planCouponList[pindex].giftDetailList.forEach((item, i) => {
        //  this.numone+=parseInt(this.planCouponList[pindex].giftDetailList[index].giftQuantity  });
        console.log(item.giftQuantity, '9999');
        this.planCouponList[pindex].giftQuantity += parseInt(item.giftQuantity);
      });
      //   this.planCouponList[pindex].consumeBonuspoint= this.planCouponList[pindex].isLimitQuantity
      console.log(this.planCouponList[pindex].giftQuantity, '99');
    },
    giftSumQuantityCheck(row, index) {
      let val = row.giftQuantity;
      //  this.planCouponList[index].giftDetailList.splice(index, 1);
      // if (val === null || val === undefined || val === "") {
      //   this.$message({showClose: true, message: "礼品库存填入数量不能为空",type: "info"});
      //   val = 1;
      // } else if (isNaN(val)){
      //   this.$message({showClose: true, message: "礼品库存填入数量格式错误",type: "info"});
      //   val = 1;
      // } else if (val < 1){
      //   this.$message({showClose: true, message: "礼品库存填入数量应大于0",type: "info"});
      //   val = 1;
      // }
      //  this.planCouponList[index].giftQuantity = parseInt(val);
      this.planCouponList[index].isLimitQuantity = 1;
      if (val < this.planCouponList[index].giveCount) {
        this.planCouponList[index].giveCount = 1;
      }
      return true;
    },
    giftSumQuantityCheckblur(row, index) {
      let val = row.giftQuantity;
      if (val === null || val === undefined || val === '') {
        this.$message({ showClose: true, message: '礼品库存填入数量不能为空', type: 'info' });
        val = 1;
      } else if (isNaN(val)) {
        this.$message({ showClose: true, message: '礼品库存填入数量格式错误', type: 'info' });
        val = 1;
      } else if (val < 1) {
        this.$message({ showClose: true, message: '礼品库存填入数量应大于0', type: 'info' });
        val = 1;
      }
    },
    giftSumQuantityCheckone(row, index) {
      let val = row;
      if (val === null || val === undefined || val === '') {
        this.$message({ showClose: true, message: '填入兑换积分数量不能为空', type: 'info' });
        val = 1;
      } else if (isNaN(val)) {
        this.$message({ showClose: true, message: '填入兑换积分数量格式错误', type: 'info' });
        val = 1;
      } else if (val < 1) {
        this.$message({ showClose: true, message: '填入兑换积分数量应大于0', type: 'info' });
        val = 1;
      }
      //  this.planCouponList[index].consumeBonuspoint = parseInt(val);
      // this.planCouponList[index].isLimitQuantity = 1;
      // if(val <this.planCouponList[index].giveCount){this.planCouponList[index].giveCount =1; }
      return true;
    },
    changeIsLimitQuantity(row, index) {
      if (row.isLimitQuantity == 0) {
        //不限
        this.planCouponList[index].giftQuantity = null;
      } else if (row.giftQuantity === null || row.giftQuantity === undefined || row.giftQuantity === '') {
        this.planCouponList[index].giftQuantity = 1;
      }
    },
    delprice(index) {
      this.planCouponList.splice(index, 1);
    },
    goback() {
      this.$router.go('-1');
    },
    quantityBlur(e, idx) {
      if (e.target.value == '') {
        return;
      }
      let flag = new RegExp('^[1-9][0-9]*$').test(e.target.value);
      if (!flag) {
        this.$message({
          message: '限领数量请输入正整数',
          type: 'warning'
        });
        e.target.value = '';
      }
    },
    numberBlur(e, name) {
      if (e.target.value == '') {
        return;
      }
      let flag = new RegExp('^[1-9][0-9]*$').test(e.target.value);
      if (!flag) {
        this.$message({
          message: name + '请输入正整数',
          type: 'warning'
        });
        e.target.value = '';
      }
    },
    qryShareActiviteInfo(uuid) {
      let data = {
        uuid: uuid
      };
      qryShareActiviteInfo(data).then(res => {
        var v = res.result;
        this.planForm.uuid = v.uuid;
        this.planForm.activityName = v.activityName;
        this.planForm.giftType = String(v.giftType);
        this.planForm.status = String(v.status);
        this.planForm.giftProperty = String(v.giftProperty);
        this.planForm.sharePic = v.sharePic;
        this.planForm.invitedPic = v.invitedPic;
        this.planForm.giftId = v.giftId;
        // this.planCouponList = res.result.promotionCouponInfo;
        this.planForm.imgListone = v.sharePic == null || v.sharePic == '' ? [] : [v.sharePic];
        this.planForm.imgListtwo = v.invitedPic == null || v.invitedPic == '' ? [] : [v.invitedPic];
        this.planForm.activityDesc = v.activityDesc;
        this.planForm.runTimeRange = [v.activityBeginTime, v.activityEndTime];
        //   this.planForm.beginTime = v.beginTime;
        //   this.planForm.endTime = v.endTime;
        this.planForm.status = v.status;
        // if (
        //   res.result.shareActivityGiftList!= null &&
        //   res.result.shareActivityGiftListlength > 0
        // ) {
        //  console.log(this.planCouponList,"op00");
        // this.planCouponList =res.records;

        this.gouJiu_options = [];
        let goodsList = res.result.bonuspointConvertActivityGiftS;

        goodsList.forEach(item => {
          if (Number(item.giftType) == 2) {
            //重新获取 组合券集合
            this.gouJiu_options.push({
              couponId: item.giftId,
              couponName: item.giftName
            });
          }
        });

        goodsList.forEach(item => {
          if (item.activityGroupDTOS && item.activityGroupDTOS.length != 0) {
            item.activityGroupDTOS_copy = item.activityGroupDTOS.map(acItem => {
              return acItem.couponId;
            });
          }
        });
        //重写礼品的购酒券信息

        // 去掉已选的
        for (var i in goodsList) {
          //   if(uuid == res.result.shareActivityGiftList[i].uuid){
          this.planCouponList.push(goodsList[i]);
          //   }
        }

        console.log(this.planCouponList, 'opop09999');
        // }
        this.getCouponList();
      });
      this.loading = false;
    },

    getCouponList() {
      let data = {
        current: this.listQuery.current,
        size: this.listQuery.size,
        condition: {
          //   activityId: this.ruleForm.uuid,
          //   goodsId: this.editGoodsId,
          status: 1,
          activityType: 2,
          giftName: this.formCoupon.giftName.trim(),
          giftCode: this.formCoupon.giftCode.trim(),
          giftType: this.formCoupon.giftType
        },
        ascs: [],
        descs: []
      };
      queryChooseGiftsList(data).then(res => {
        this.couponList = res.records;
        this.total3 = res.total;
        // 去掉已选的
        if (this.planCouponList != null) {
          for (var i in this.planCouponList) {
            this.removeByValue(this.couponList, 'uuid', this.planCouponList[i].uuid);
          }
        }
      });
      this.loading = false;
    },

    planCouponChecked(row) {
      console.log('k');
      this.lsPlanCouponList = row;
    },
    couponChecked(row) {
      this.lsCouponList = row;

      console.log('k88');
      //   if (this.lsCouponList.length > 1) {
      //       this.$refs.singleTable.toggleRowSelection(this.lsCouponList[0]);
      //       }
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.getCouponList();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.getCouponList();
    },
    selectCoupon() {
      // 显示选择卡券对话框
      this.dialogVisibleCoupon = !this.dialogVisibleCoupon;
      this.getCouponList();
    },
    deleteCoupon() {
      // 删除已选择的卡券
      this.couponList = this.couponList.concat(this.lsPlanCouponList);
      for (var i in this.lsPlanCouponList) {
        this.removeByValue(this.planCouponList, 'uuid', this.lsPlanCouponList[i].uuid);
      }
    },
    couponConfirm() {
      this.dialogVisibleCoupon = !this.dialogVisibleCoupon;
      //this.planCouponList.concat(this.lsCouponList);
      for (var i in this.lsCouponList) {
        this.$set(this.lsCouponList[i], 'giveCount', 1);
        this.$set(this.lsCouponList[i], 'isLimitQuantity', 0);
        this.$set(this.lsCouponList[i], 'giftDetailList', []);
        this.planCouponList.push(this.lsCouponList[i]);
      }
      console.log(this.planCouponList, 'op99');
      for (var i in this.planCouponList) {
        if (this.planCouponList[i].giftId == null || this.planCouponList[i].giftId == undefined) {
          this.planCouponList[i].giftId = this.planCouponList[i].uuid;
          this.planCouponList[i].uuid = null;
        }
      }
      console.log('选择的礼品', this.planCouponList);

      //获取 购酒卡券组合列表
      this.gouJiu_options = [];
      this.planCouponList.forEach(item => {
        if (Number(item.giftType) == 2) {
          this.gouJiu_options.push({
            couponId: item.giftId,
            couponName: item.giftName
          });
        }
      });
    },

    submitForm(formName) {
      // 保存奖品
      console.log(this.shijianflag, 'oo');

      if (this.planForm.activityName == '' || this.planForm.activityName == null) {
        this.$message.error('请输入活动名称');
        return;
      }

      // if (this.planForm.imgListone.length == 0) {
      //   this.$message.error("请上传活动分享页主图");
      //   return;
      // }
      //     if (this.planForm.imgListtwo.length == 0) {
      //   this.$message.error("请上传活动受邀页主图");
      //   return;planCouponList
      // }
      //    if ( this.planCouponList.length == 0) {
      //   this.$message.error("请选择赠品");
      //   return;
      // }

      if (this.planCouponList.length == 0) {
        this.$message.error('请添加礼品');
        return;
      }
      if (this.planCouponList[0].giftDetailList.length == 0) {
        this.$message.error('请添加礼品时间段');
        return;
      }

      if (this.planForm.activityDesc == '' || this.planForm.activityDesc == null) {
        this.$message.error('请输入积分兑换活动说明');
        return;
      }

      if (this.shijianflag == false) {
        this.$message({
          message: '当前已选区间存在重复时间',
          type: 'warning'
        });
        return;
      }

      let r = new RegExp('^[1-9][0-9]*$');
      for (var ii in this.planCouponList) {
        var flag1 = true;
        var flag2 = true;
        var flag3 = true;
        let giftStyle = this.planCouponList[ii].giftStyle;
        let validDateType = this.planCouponList[ii].validDateType;
        let dayCount = this.planCouponList[ii].dayCount;
        let validDateRange = this.planCouponList[ii].validDateRange;
        if (giftStyle != '1' && giftStyle != '3' && giftStyle != '5') {
          if (validDateType == undefined) {
            flag1 = false;
            break;
          }
        }
        if (giftStyle != '1' && giftStyle != '3' && giftStyle != '5' && validDateType == '1') {
          if (dayCount == null || dayCount == '' || !r.test(dayCount)) {
            flag3 = false;
            break;
          }
        }
        if (giftStyle != '1' && giftStyle != '3' && giftStyle != '5' && validDateType == '2') {
          if (validDateRange == null || validDateRange.length == 0 || validDateRange[0] == '') {
            flag2 = false;
            break;
          }
        }
      }
      // if (!flag1) {
      //   this.$message({ message: "请正确选择有效期类型按钮", type: "warning" }); return;
      // }
      // if (!flag2) {
      //   this.$message({ message: "请正确输入有效日期,且不能为空", type: "warning" }); return;
      // }
      // if (!flag3) {
      //   this.$message({ message: "请正确输入有效天数,且不能为空", type: "warning" }); return;
      // }

      for (var i in this.planCouponList) {
        // 删除提交时不需要的属性或容易导致错误的属性
        //         if(this.$route.params.disabledFlag!==false){
        //  if (
        //           this.planCouponList[i].uuid != null &&
        //           this.planCouponList[i].uuid != undefined
        //         ) {
        //           this.planCouponList[i].giftId = this.planCouponList[i].uuid;
        //           this.planCouponList[i].uuid=""
        //         }
        //         }

        delete this.planCouponList[i].createTime;
        delete this.planCouponList[i].createUser;
        delete this.planCouponList[i].updateTime;
        delete this.planCouponList[i].updateUser;
        // delete this.planCouponList[i].beginTime;
        // delete this.planCouponList[i].endTime;
        delete this.planCouponList[i].comments;
        delete this.planCouponList[i].validCreateDate;
        delete this.planCouponList[i].validEndDate;
      }

      // 购酒券组合信息
      this.planCouponList.forEach(item => {
        if (Number(item.giftType == 2)) {
          item.activityGroupDTOS = [];
          item.activityGroupDTOS_copy.forEach(cItem => {
            this.gouJiu_options.forEach(pItem => {
              if (cItem == pItem.couponId) {
                item.activityGroupDTOS.push(pItem);
              }
            });
          });
        }
      });

      //判断个人限购是否填了 giftDetailList
      let inputFlag = false;
      this.planCouponList.forEach(item => {
        item.giftDetailList.forEach(gitem => {
          if (!gitem.buyLimitQuantity || gitem.buyLimitQuantity <= 0) {
            inputFlag = true;
          }
        });
      });

      if (inputFlag) {
        this.$message({ message: '请填写个人限购数量', type: 'warning' });
        return false;
      }

      console.log('选了购酒券后的', this.planCouponList);
      this.planForm.bonuspointConvertActivityGiftS = this.planCouponList;

      //   if (this.planForm.giftType=="2"){
      //   this.planForm.giftId = this.planForm.shareActivityGiftList[0].uuid;
      //   this.planForm.giftName = this.planForm.shareActivityGiftList[0].giftName;
      //   }

      // this.planForm.sharePic = this.planForm.imgListone[0]
      // this.planForm.invitedPic = this.planForm.imgListtwo[0]
      if (this.planForm.runTimeRange != null && this.planForm.runTimeRange.length == 2) {
        this.planForm.activityBeginTime = this.planForm.runTimeRange[0];
        this.planForm.activityEndTime = this.planForm.runTimeRange[1];
      }
      for (var i in this.planCouponList) {
        //  if (
        //       this.planCouponList[i].giftId == null ||
        //       this.planCouponList[i].giftId == undefined
        //     ) {

        if (this.planCouponList[i].validDateType == 2) {
          this.planCouponList[i].validBeginDate = this.planCouponList[i].validDateRange[0];
          this.planCouponList[i].validEndDate = this.planCouponList[i].validDateRange[1];
          ``;
        }

        // }
      }

      this.keepAlive = false;
      if (this.planForm.uuid == '') {
        this.planForm.uuid = null;
        saveGiftActivityWithGoods(this.planForm).then(res => {
          if (res.code == '1') {
            this.$message({
              showClose: true,
              message: '保存成功',
              type: 'success'
            });
            //   this.$router.push("prizeLibrary");
            this.$router.push('IntegralActivity');
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            });
          }
        });
      } else {
        saveGiftActivityWithGoods(this.planForm).then(res => {
          if (res.code == '1') {
            this.$message({
              showClose: true,
              message: '修改成功',
              type: 'success'
            });
            //   this.$router.push("prizeLibrary");
            this.$router.push('IntegralActivity');
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            });
          }
        });
      }
    },
    // 数组，属性，属性值
    removeByValue: function(arr, attr, value) {
      if (arr == null) {
        return;
      }
      var index = -1;
      for (var i in arr) {
        if (arr[i][attr] == value) {
          index = i;
          break;
        }
      }
      if(index>-1){
        arr.splice(index, 1);
      }
    },
    handleRemove(file, fileList) {
      let newfileList = [];
      fileList.forEach((item, i) => {
        let data = {
          name: item.name,
          url: item.url
        };
        newfileList.push(data);
      });
      this.fileList = newfileList;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$router.push('IntegralActivity');
    },

    changeLeve(v, index) {
      let array = v.memberLevelIdList;
      if (
        array.some((item, i) => {
          return item == '1';
        })
      ) {
        this.planCouponList[index].memberLevelIdList = this.optionsarray.map((item, i) => item.uuid);
      }
    }
  },
  beforeRouteLeave(to, from, next) {
    // 设置下一个路由的 meta
    to.meta.keepAlive = this.keepAlive; // C 跳转到 A 时让 A 不缓存，即刷新
    next();
  }
};
</script>
<style lang="less" scoped>
.el-col-20 {
  border: 1px solid #ebeef5;
  margin-left: 150px;
  width: 60%;
}

.el-input {
  width: 60%;
}

.search-form-inline {
  border: 1px solid #ebeef5;
  line-height: 30px;
  padding: 12px;
  margin-bottom: 5px;
}

thead {
  .el-table-column--selection {
    .cell {
      display: none;
    }
  }
}

// #giftActivity .giftTable table .el-radio-button-inner-child .el-radio-button__inner{
//   padding:0;
// }
// #giftActivity .giftTable table .el-radio-button-inner-child .el-input__inner{
//   border:none;
//   height:30px;
// }
#giftActivity /deep/ .giftTable table th,
#giftActivity .giftTable table td {
  border: 1px solid #ebeef5;
}

#giftActivity /deep/ .giftTable table th {
  background: #eef1f6;
  color: #606266;
}

#giftActivity /deep/ .giftTable table .el-radio-button-inner-child .el-radio-button__inner {
  padding: 0;
}

#giftActivity /deep/ .giftTable table .el-radio-button-inner-child .el-input__inner {
  border: none;
  height: 30px;
}

#giftActivity /deep/ .el-dialog__body {
  padding-top: 0px;
}

#giftActivityGoodsTable /deep/ .el-input__inner[type='number'] {
  padding: 0;
}

.el-select__tags /deep/ {
  width: 100%;
  max-width: 200px;
}
</style>
